package com.example.commentservice.utils;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * @Description: TODO
 * @Author Mr.Tang
 */
public class TimeUtils {

    /**
     * 得到距今的时间
     * @param date 目标时间
     * @return 拼接字符串
     */
    public static String toNow(LocalDateTime date) {
        // 现在的时间
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(date, now);

        // 分情况判断返回
        long min = duration.toMinutes();
        if (min == 0) {
            long millis = duration.toMillis();
            return millis + " 秒前";
        }
        if (min < 60) {
            return min + " 分钟前";
        }
        long hours = duration.toHours();
        if (hours < 24) {
            return hours + " 小时前";
        }
        long days = duration.toDays();
        if (days < 30) {
            return days + " 天前";
        }
        long mon = days / 30;
        if (mon < 12) {
            return mon + " 个月前";
        }

        long year = days / 356;
        return year + " 年前";

    }
}
